/*
========================================================================
 Name        : CallDetailTrackerGenerateReportForm.cpp
 Author      : ${author}
 Copyright   : ${copyright}
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <barsread.h>
#include <stringloader.h>
#include <eiklabel.h>
#include <eikenv.h>
#include <gdi.h>
#include <eikmfne.h>
#include <aknviewappui.h>
#include <eikappui.h>
#include <avkon.hrh>
#include <CallDetailTracker.rsg>
// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]
#include "CallDetailTrackerGenerateReportForm.h"
#include "CallDetailTrackerGenerateReportFormView.h"
#include "CallDetailTracker.hrh"
#include "CallDetailTrackerMainForm.hrh"
#include "CallDetailTrackerViewReportOptionsForm.hrh"
#include "CallDetailTrackerGenerateReportForm.hrh"
#include "CallDetailTrackerProgressingForm.hrh"
#include "CallDetailTrackerDeleteCallLogSelector.hrh"
#include "CallDetailTrackerMainControler.hrh"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]
#include "NamedDataBase.h"
#include "FileManager.h"
#include "CallDetailTrackerViewReportOptionsForm.h"
/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CCallDetailTrackerGenerateReportForm::CCallDetailTrackerGenerateReportForm()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	iLabel1 = NULL;
	iLabel2 = NULL;
	iStartDate = NULL;
	iEndDate = NULL;
	// ]]] end generated region [Generated Contents]
	
	}
/** 
 * Destroy child controls.
 */
CCallDetailTrackerGenerateReportForm::~CCallDetailTrackerGenerateReportForm()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	delete iLabel1;
	iLabel1 = NULL;
	delete iLabel2;
	iLabel2 = NULL;
	delete iStartDate;
	iStartDate = NULL;
	iControlEventDispatch.Close();		
	delete iEndDate;
	iEndDate = NULL;
	// ]]] end generated region [Generated Contents]
	
	}
				
/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CCallDetailTrackerGenerateReportForm
 */
CCallDetailTrackerGenerateReportForm* CCallDetailTrackerGenerateReportForm::NewL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CCallDetailTrackerGenerateReportForm* self = CCallDetailTrackerGenerateReportForm::NewLC( 
			aRect, 
			aParent, 
			aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CCallDetailTrackerGenerateReportForm
 */
CCallDetailTrackerGenerateReportForm* CCallDetailTrackerGenerateReportForm::NewLC( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CCallDetailTrackerGenerateReportForm* self = new ( ELeave ) CCallDetailTrackerGenerateReportForm();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}
			
/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */ 
void CCallDetailTrackerGenerateReportForm::ConstructL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	if ( aParent == NULL )
	    {
		CreateWindowL();
	    }
	else
	    {
	    SetContainerWindowL( *aParent );
	    }
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	SetRect( aRect );
	ActivateL();
	// [[[ begin generated region: do not modify [Post-ActivateL initializations]
	// ]]] end generated region [Post-ActivateL initializations]
	
	}
			
/**
* Return the number of controls in the container (override)
* @return count
*/
TInt CCallDetailTrackerGenerateReportForm::CountComponentControls() const
	{
	return ( int ) ELastControl;
	}
				
/**
* Get the control with the given index (override)
* @param aIndex Control index [0...n) (limited by #CountComponentControls)
* @return Pointer to control
*/
CCoeControl* CCallDetailTrackerGenerateReportForm::ComponentControl( TInt aIndex ) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	switch ( aIndex )
		{
		case ELabel1:
			return iLabel1;
		case ELabel2:
			return iLabel2;
		case EStartDate:
			return iStartDate;
		case EEndDate:
			return iEndDate;
		}
	// ]]] end generated region [Generated Contents]
	
	// handle any user controls here...
	
	return NULL;
	}
				
/**
 *	Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */				
void CCallDetailTrackerGenerateReportForm::SizeChanged()
	{
	CCoeControl::SizeChanged();
	LayoutControls();
	// [[[ begin generated region: do not modify [Generated Contents]
			
	// ]]] end generated region [Generated Contents]
	
	}
// [[[ begin generated function: do not modify
/**
 * Layout components as specified in the UI Designer
 */
void CCallDetailTrackerGenerateReportForm::LayoutControls()
	{
	iLabel1->SetExtent( TPoint( 1, 43 ), TSize( 99, 31 ) );
	iLabel2->SetExtent( TPoint( -1, 103 ), TSize( 109, 33 ) );
	iStartDate->SetExtent( TPoint( 130, 48 ), TSize( 76, 18 ) );
	iEndDate->SetExtent( TPoint( 133, 108 ), TSize( 76, 18 ) );
	}
// ]]] end generated function


/**
 *	Handle key events.
 */				
TKeyResponse CCallDetailTrackerGenerateReportForm::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	
	// ]]] end generated region [Generated Contents]
	
	if ( iFocusControl != NULL
		&& iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
		{
		return EKeyWasConsumed;
		}
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
	}
				
// [[[ begin generated function: do not modify
/**
 *	Initialize each control upon creation.
 */				
void CCallDetailTrackerGenerateReportForm::InitializeControlsL()
	{
	iLabel1 = new ( ELeave ) CEikLabel;
	iLabel1->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_DETAIL_TRACKER_GENERATE_REPORT_FORM_LABEL1 );
		iLabel1->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iLabel2 = new ( ELeave ) CEikLabel;
	iLabel2->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_DETAIL_TRACKER_GENERATE_REPORT_FORM_LABEL2 );
		iLabel2->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iStartDate = new ( ELeave ) CEikDateEditor;
	iStartDate->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_DETAIL_TRACKER_GENERATE_REPORT_FORM_START_DATE );
		iStartDate->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iStartDate->SetDate( TTime( TDateTime( 2000, EJanuary, 0, 0, 0, 0, 0 ) ) );
	iStartDate->SetFont( iEikonEnv->LegendFont() );
	iStartDate->SetObserver( this );
	AddControlEventHandlerL( 
			iStartDate, 
			EEventStateChanged, 
			&CCallDetailTrackerGenerateReportForm::HandleStartDateStateChangedL );
	iEndDate = new ( ELeave ) CEikDateEditor;
	iEndDate->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_DETAIL_TRACKER_GENERATE_REPORT_FORM_END_DATE );
		iEndDate->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iEndDate->SetDate( TTime( TDateTime( 2000, EJanuary, 0, 0, 0, 0, 0 ) ) );
	iEndDate->SetFont( iEikonEnv->LegendFont() );
	iEndDate->SetObserver( this );
	AddControlEventHandlerL( 
			iEndDate, 
			EEventStateChanged, 
			&CCallDetailTrackerGenerateReportForm::HandleEndDateStateChangedL );
	AddControlEventHandlerL( 
			iEndDate, 
			EEventRequestFocus, 
			&CCallDetailTrackerGenerateReportForm::HandleEndDateRequestingFocusL );
	
	iStartDate->SetFocus( ETrue );
	iFocusControl = iStartDate;
	
	}
// ]]] end generated function

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CCallDetailTrackerGenerateReportForm::HandleResourceChange( TInt aType )
	{
	CCoeControl::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( ECallDetailTrackerGenerateReportFormViewId ) )->ClientRect() );
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
				
/**
 *	Draw container contents.
 */				
void CCallDetailTrackerGenerateReportForm::Draw( const TRect& aRect ) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
	
	// ]]] end generated region [Generated Contents]
	
	}
				
/** 
 * Override of the HandleControlEventL virtual function
 */
void CCallDetailTrackerGenerateReportForm::HandleControlEventL( 
		CCoeControl* aControl, 
		TCoeEvent anEventType )
	{
	for (int i = 0; i < iControlEventDispatch.Count(); i++)
		{
		const TControlEventDispatch& currEntry = iControlEventDispatch[i];
		if ( currEntry.src == aControl && currEntry.event == anEventType )
			{
			( this->*currEntry.handler )( aControl, anEventType );
			break;
			}
		}
	}
/** 
 * Helper function to register MCoeControlObserver event handlers
 */
void CCallDetailTrackerGenerateReportForm::AddControlEventHandlerL( 
		CCoeControl* aControl, 
		TCoeEvent anEvent, 
		ControlEventHandler aHandler )
	{
	TControlEventDispatch entry;
	entry.src = aControl;
	entry.event = anEvent;
	entry.handler = aHandler;
	TInt err = iControlEventDispatch.Append( entry );
	User::LeaveIfError( err );
	}
			
/** 
 * Handle the EEventRequestFocus event.
 */
void CCallDetailTrackerGenerateReportForm::HandleEndDateRequestingFocusL( CCoeControl* /* aControl */, TCoeEvent /* anEvent */ )
	{
	// TODO: implement requestingFocus event handler
	}
				
/** 
 * Handle the EEventStateChanged event.
 */
void CCallDetailTrackerGenerateReportForm::HandleStartDateStateChangedL( 
		CCoeControl* /* aControl */, 
		TCoeEvent /* anEvent */ )
	{
	// TODO: implement stateChanged event handler
	}
				
/** 
 * Handle the EEventStateChanged event.
 */
void CCallDetailTrackerGenerateReportForm::HandleEndDateStateChangedL( 
		CCoeControl* /* aControl */, 
		TCoeEvent /* anEvent */ )
	{
	// TODO: implement stateChanged event handler
	}
void CCallDetailTrackerGenerateReportForm::GetUserDates()
    {
	 TBuf<20> startDate; //A text variable to store start date
	 TBuf<20> endDate;//A text variable to store end date
	   
	 startDate.Zero();//For some reason this is set to be zero
	 endDate.Zero();//For some reason this is set to be zero
	   
	 TDateTime Std;//DateTime object to get start date
	 TDateTime Etd;//DateTime object to get start date
	   
	 Std=iStartDate->Date().DateTime(); //Store the start date of the user
	 Etd=iEndDate->Date().DateTime(); //Store the end date of the user
	   
	 startDate.Format(_L("%04d/%02d/%02d"),Std.Year(), Std.Month()+1, Std.Day()+1); //Convert the start date in to text
	 endDate.Format(_L("%04d/%02d/%02d"),Etd.Year(), Etd.Month()+1, Etd.Day()+1); ; //Convert the end date in to text 
	  
	  
    //Send the details to the classs object
    ReportGeneratorDetails* iRepDates =new (ELeave) ReportGeneratorDetails;
    iRepDates->setFromDate(startDate);
    iRepDates->setToDate(endDate);
    
    CCallDetailTrackerViewReportOptionsForm* iviewOptions=new (ELeave) CCallDetailTrackerViewReportOptionsForm;
    iviewOptions->SetRequestedDates(iRepDates);
}
				
